$(function () {
    initDate();
    initFull();
    // initSwitch();
    $("#start_date,#end_date").change(function(){
        let start = $("#start_date").val();
    let end = $("#end_date").val();
    if (start != '' && end != '') {
        if (start >= end) {
            $("#start_date,#end_date").unbind("change")
            layer.alert('开始时间不能大于结束时间');
        }
    }
})
})
var d = 'http://116.228.185.63:8040';
function initDomain(domain) {
    d = domain;
}
/**
 * 商铺列表初始化
 * @param url
 * @param selectClass
 * @param selectedDataStr
 */
function initListBox(url, selectClass, selectedDataStr) {
    var paramData = {
        'pageSize': 999
    }
    $.ajax({
        url: url,
        type: 'get',
        async: true,
        success: function (returnData) {
            var objs = returnData.data.list;
            $(objs).each(function () {
                var o = document.createElement("option");
                o.value = this['id'];
                o.text = this['storeName'];
                if ("undefined" != typeof (selectedDataStr) && selectedDataStr != "") {
                    var selectedDataArray = selectedDataStr.split(',');
                    $.each(selectedDataArray, function (i, val) {
                        if (o.value == val) {
                            o.selected = 'selected';
                            return false;
                        }
                    });
                }
                $("." + selectClass + "")[0].options.add(o);
            });
            $('.' + selectClass + '').bootstrapDualListbox({
                nonSelectedListLabel: '未选项',
                selectedListLabel: '已选项',
                // filterTextClear: '×',
                filterPlaceHolder: '过滤条件',
                preserveSelectionOnMove: 'moved',
                moveOnSelect: false,
                moveAllLabel: '添加全部',
                btnMoveText: '<i class="glyphicon glyphicon glyphicon-chevron-right"></i>',
                btnRemoveText: '<i class=" glyphicon glyphicon-chevron-left"></i>',
                btnMoveAllText: '<i class="glyphicon glyphicon glyphicon-chevron-right"></i><i class="glyphicon glyphicon glyphicon-chevron-right"></i>',
                btnRemoveAllText: '<i class="glyphicon glyphicon-chevron-left"></i><i class="glyphicon glyphicon-chevron-left"></i>',
                moveSelectedLabel: '添加选中',
                removeAllLabel: '移除全部',
                removeSelectedLabel: '移除选中',
                infoText: '{0} 项',
                btnClass: 'btn-link',
                infoTextFiltered: '从 {1} 项 筛选 {0} 项',
                infoTextEmpty: '无'
            });
        },
        error: function (e) {
            alert(e.msg);
        }
    });
}

/**
 * 业态列表初始化
 * @param url
 * @param selectClass
 * @param selectedDataStr
 */
function initCommercialListBox(url, selectClass, selectedDataStr) {
    var paramData = {
        'pageSize': 999
    }
    $.ajax({
        url: url,
        type: 'get',
        async: true,
        success: function (returnData) {
            var objs = returnData.data.list;
            $(objs).each(function () {
                var o = document.createElement("option");
                o.value = this['id'];
                o.text = this['name'];
                if ("undefined" != typeof (selectedDataStr) && selectedDataStr != "") {
                    var selectedDataArray = selectedDataStr.split(',');
                    $.each(selectedDataArray, function (i, val) {
                        if (o.value == val) {
                            o.selected = 'selected';
                            return false;
                        }
                    });
                }
                $("." + selectClass + "")[0].options.add(o);
            });
            $('.' + selectClass + '').bootstrapDualListbox({
                nonSelectedListLabel: '未选项',
                selectedListLabel: '已选项',
                // filterTextClear: '×',
                filterPlaceHolder: '过滤条件',
                preserveSelectionOnMove: 'moved',
                moveOnSelect: false,
                moveAllLabel: '添加全部',
                btnMoveText: '<i class="glyphicon glyphicon glyphicon-chevron-right"></i>',
                btnRemoveText: '<i class=" glyphicon glyphicon-chevron-left"></i>',
                btnMoveAllText: '<i class="glyphicon glyphicon glyphicon-chevron-right"></i><i class="glyphicon glyphicon glyphicon-chevron-right"></i>',
                btnRemoveAllText: '<i class="glyphicon glyphicon-chevron-left"></i><i class="glyphicon glyphicon-chevron-left"></i>',
                moveSelectedLabel: '添加选中',
                removeAllLabel: '移除全部',
                removeSelectedLabel: '移除选中',
                infoText: '{0} 项',
                btnClass: 'btn-link',
                infoTextFiltered: '从 {1} 项 筛选 {0} 项',
                infoTextEmpty: '无'
            });
        },
        error: function (e) {
            alert(e.msg);
        }
    });
}

/**
 * 表单初始化
 */
$.fn.setForm = function (jsonValue) {
    var obj = this;
    $.each(jsonValue, function (name, ival) {
        var $oinput = obj.find("input[name=" + name + "]");
        if ($oinput.attr("type") == "checkbox") {
            if (ival !== null) {
                var checkboxObj = $("[name=" + name + "]");
                var checkArray = ival.split(";");
                for (var i = 0; i < checkboxObj.length; i++) {
                    for (var j = 0; j < checkArray.length; j++) {
                        if (checkboxObj[i].value == checkArray[j]) {
                            checkboxObj[i].click();
                        }
                    }
                }
            }
        } else if ($oinput.attr("type") == "radio") {
            $oinput.each(function () {
                var radioObj = $("[name=" + name + "]");
                for (var i = 0; i < radioObj.length; i++) {
                    if (radioObj[i].value == ival) {
                        radioObj[i].click();
                    }
                }
            });
        } else if ($oinput.attr("type") == "textarea") {
            obj.find("[name=" + name + "]").html(ival);
        } else {
            obj.find("[name=" + name + "]").val(ival);
        }
    })
}

function initFull() {
    //设置为中文
    Simditor.locale = 'zh-CN';
    $(".myEditor").each(function(i, e){
        new Simditor({
            textarea: $(e),  //textarea的id
            placeholder: '',
            toolbar: ['title', 'bold', 'italic', 'underline', 'strikethrough', 'color', 'ol', 'ul', 'blockquote', 'code', 'table', 'link', 'image', 'hr', 'indent', 'outdent','alignment'], //工具条都包含哪些内容
            pasteImage: true,//允许粘贴图片
            //defaultImage: '/simditor/images/image.png',//编辑器插入的默认图片，此处可以删除
            upload: {
                url: d + "/quanquan-resource/image/upload",
                params: null, //键值对,指定文件上传接口的额外参数,上传的时候随文件一起提交
                fileKey: 'file', //服务器端获取文件数据的参数名
                connectionCount: 3,
                leaveConfirm: '正在上传文件'
            }
        });
})
    ;
}

function initDate() {
    $('#start_date, #end_date,#show_start_date,#birthday,#register_start_date,#register_end_date,#birthday_start,#birthday_end').datepicker({
        autoclose: true,
        clearBtn: true, //清除按钮
        todayBtn: false, //今日按钮
        format: "yyyy-mm-dd",
        todayHighlight: true
    });
    $('#coupon_start_date, #coupon_end_date, #buy_time_start, #buy_time_end,#show_start_time,#show_end_time').datetimepicker({
        weekStart: 0, //一周从哪一天开始
        todayBtn: 1, //
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
}

/*
function initSwitch() {
    var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem,{size:"small"});
}*/
$.fn.loadData = function (jsonStr) {
    var obj = eval(jsonStr);
    var key, value, tagName, type, arr;
    for (x in obj) {
        key = x;
        value = obj[x];
        try {
            // $("[id='" + key + "']").each(function () {
            $("[name='" + key + "'],[name='" + key + "[]']").each(function () {
                tagName = $(this)[0].tagName;
                type = $(this).attr('type');
                if (tagName == 'img') {
                    if (null != value && '' != value) {
                        $(this).attr('src', value);
                    }
                }
                if (tagName == 'INPUT') {
                    if (type == 'radio') {
                        $(this).attr('checked', $(this).val() == value || ($(this).val() == '' && value == null));
                    } else if (type == 'checkbox') {
                        arr = value.split(',');
                        for (var i = 0; i < arr.length; i++) {
                            if ($(this).val() == arr[i]) {
                                $(this).attr('checked', true);
                                break;
                            }
                        }
                    } else {
                        $(this).val(value);
                    }
                } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {
                    $(this).val(value);
                } else if (tagName == 'SPAN') {
                    $(this).html(value);
                }
            });
        } catch (e) {
        }
    }
};

function NavToggle() {
    $(".navbar-minimalize").trigger("click")
}
function SmoothlyMenu() {
    $("body").hasClass("mini-navbar") ? $("body").hasClass("fixed-sidebar") ? ($("#side-menu").hide(), setTimeout(function () {
        $("#side-menu").fadeIn(500)
    }, 300)) : $("#side-menu").removeAttr("style") : ($("#side-menu").hide(), setTimeout(function () {
        $("#side-menu").fadeIn(500)
    }, 100))
}
function localStorageSupport() {
    return "localStorage" in window && null !== window.localStorage
}
layer.config({
    extend: ["extend/layer.ext.js", "skin/layer.ext.css"],
    skin: "layer-ext-moon"
}), $(document).ready(function () {
    function e() {
        var e = $("body > #wrapper").height() - 61;
        $(".sidebard-panel").css("min-height", e + "px")
    }

    $("#side-menu").metisMenu(), $(".right-sidebar-toggle").click(function () {
        $("#right-sidebar").toggleClass("sidebar-open")
    }), $(".sidebar-container").slimScroll({
        height: "100%",
        railOpacity: .4,
        wheelStep: 10
    }), $(".open-small-chat").click(function () {
        $(this).children().toggleClass("fa-comments").toggleClass("fa-remove"), $(".small-chat-box").toggleClass("active")
    }), $(".small-chat-box .content").slimScroll({
        height: "234px",
        railOpacity: .4
    }), $(".check-link").click(function () {
        var e = $(this).find("i"), a = $(this).next("span");
        return e.toggleClass("fa-check-square").toggleClass("fa-square-o"), a.toggleClass("todo-completed"), !1
    }), $(function () {
        $(".sidebar-collapse").slimScroll({height: "100%", railOpacity: .9, alwaysVisible: !1})
    }), $(".navbar-minimalize").click(function () {
        $("body").toggleClass("mini-navbar"), SmoothlyMenu()
    }), e(), $(window).bind("load resize click scroll", function () {
        $("body").hasClass("body-small") || e()
    }), $(window).scroll(function () {
        $(window).scrollTop() > 0 && !$("body").hasClass("fixed-nav") ? $("#right-sidebar").addClass("sidebar-top") : $("#right-sidebar").removeClass("sidebar-top")
    }), $(".full-height-scroll").slimScroll({height: "100%"}), $("#side-menu>li").click(function () {
        $("body").hasClass("mini-navbar") && NavToggle()
    }), $("#side-menu>li li a").click(function () {
        $(window).width() < 769 && NavToggle()
    }), $(".nav-close").click(NavToggle), /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent) && $("#content-main").css("overflow-y", "auto")
}), $(window).bind("load resize", function () {
    $(this).width() < 769 && ($("body").addClass("mini-navbar"), $(".navbar-static-side").fadeIn())
}), $(function () {
    if ($("#fixednavbar").click(function () {
        $("#fixednavbar").is(":checked") ? ($(".navbar-static-top").removeClass("navbar-static-top").addClass("navbar-fixed-top"), $("body").removeClass("boxed-layout"), $("body").addClass("fixed-nav"), $("#boxedlayout").prop("checked", !1), localStorageSupport && localStorage.setItem("boxedlayout", "off"), localStorageSupport && localStorage.setItem("fixednavbar", "on")) : ($(".navbar-fixed-top").removeClass("navbar-fixed-top").addClass("navbar-static-top"), $("body").removeClass("fixed-nav"), localStorageSupport && localStorage.setItem("fixednavbar", "off"))
    }), $("#collapsemenu").click(function () {
        $("#collapsemenu").is(":checked") ? ($("body").addClass("mini-navbar"), SmoothlyMenu(), localStorageSupport && localStorage.setItem("collapse_menu", "on")) : ($("body").removeClass("mini-navbar"), SmoothlyMenu(), localStorageSupport && localStorage.setItem("collapse_menu", "off"))
    }), $("#boxedlayout").click(function () {
        $("#boxedlayout").is(":checked") ? ($("body").addClass("boxed-layout"), $("#fixednavbar").prop("checked", !1), $(".navbar-fixed-top").removeClass("navbar-fixed-top").addClass("navbar-static-top"), $("body").removeClass("fixed-nav"), localStorageSupport && localStorage.setItem("fixednavbar", "off"), localStorageSupport && localStorage.setItem("boxedlayout", "on")) : ($("body").removeClass("boxed-layout"), localStorageSupport && localStorage.setItem("boxedlayout", "off"))
    }), $(".s-skin-0").click(function () {
        return $("body").removeClass("skin-1"), $("body").removeClass("skin-2"), $("body").removeClass("skin-3"), !1
    }), $(".s-skin-1").click(function () {
        return $("body").removeClass("skin-2"), $("body").removeClass("skin-3"), $("body").addClass("skin-1"), !1
    }), $(".s-skin-3").click(function () {
        return $("body").removeClass("skin-1"), $("body").removeClass("skin-2"), $("body").addClass("skin-3"), !1
    }), localStorageSupport) {
        var e = localStorage.getItem("collapse_menu"), a = localStorage.getItem("fixednavbar"), o = localStorage.getItem("boxedlayout");
        "on" == e && $("#collapsemenu").prop("checked", "checked"), "on" == a && $("#fixednavbar").prop("checked", "checked"), "on" == o && $("#boxedlayout").prop("checked", "checked")
    }
    if (localStorageSupport) {
        var e = localStorage.getItem("collapse_menu"), a = localStorage.getItem("fixednavbar"), o = localStorage.getItem("boxedlayout"), l = $("body");
        "on" == e && (l.hasClass("body-small") || l.addClass("mini-navbar")), "on" == a && ($(".navbar-static-top").removeClass("navbar-static-top").addClass("navbar-fixed-top"), l.addClass("fixed-nav")), "on" == o && l.addClass("boxed-layout")
    }
});
